var util = require("../../utils/util.js");
const app = getApp()
Component({
    properties: {
        // 这里定义了innerText属性，属性值可以在组件使用时指定
    },
    data: {

        isShowLogin: app.globalData.isShowLogin,
        isChecked: !1
    },
    methods: {
        openLoginModal: function() {
            this.setData({
                isShowLogin: !0
            })
        },
        closeLoginModal: function() {
            this.setData({
                isShowLogin: !1
            });
            var myEventDetail = {} // detail对象，提供给事件监听函数
            var myEventOption = {} // 触发事件的选项
            this.triggerEvent('closeEvent', myEventDetail, myEventOption)
        },
        changeChecked: function() {
            const _this = this;
            _this.setData({
                isChecked: !_this.data.isChecked
            })
        },
        userAgreement: function() {
            wx.navigateTo({
                url: '/pages/agreement/agreement?orgin=' + 'userAgreement'
            })
        },
        privacyPolicy: function() {
            wx.navigateTo({
                url: '/pages/agreement/agreement?orgin=' + 'privacyPolicy'
            })
        },
        onWechatLogin(e) {
            const _this = this;
            if (_this.data.isChecked) {
                return false;
            }
            if (e.detail.errMsg !== 'getUserInfo:ok') {
                if (e.detail.errMsg === 'getUserInfo:fail auth deny') {
                    return false
                }
                wx.showToast({
                    title: '微信登录失败',
                })
                return false
            }
            util.login().then((res) => {
                return util.request(getApp().api.user.login, {
                    code: res,
                    userInfo: e.detail.userInfo
                }, 'POST');
            }).then((res) => {
                if (res.code !== 200) {
                    wx.showToast({
                        title: '微信登录失败',
                    })
                    return false;
                }
                // 设置用户信息
                _this.setData({
                    userInfo: res.data.userInfo,
                    isShowLogin: !1
                });
                _this.triggerEvent('login', { userInfo: _this.data.userInfo })
                    // app.globalData.userInfo = res.data.userInfo;
                    // app.globalData.token = res.data.token;
                    // wx.setStorageSync('userInfo', JSON.stringify(res.data.userInfo));
                wx.setStorageSync('userInfo', res.data.userInfo);
                // wx.setStorageSync('token', res.data.info.session_key);
                wx.setStorageSync('openid', res.data.userInfo.openId);
            }).catch((err) => {
                console.log(err)
            })
        },
    }
})